package com.zyx.sercurity.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyx.sercurity.entity.Users;
import com.zyx.sercurity.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("userName",username);
        Users users = userMapper.selectOne(wrapper);

        if (users == null){
            throw new UsernameNotFoundException("用户名不存在!");
        }

        List<GrantedAuthority> role = AuthorityUtils.commaSeparatedStringToAuthorityList("admins");
        return new User(users.getUsername(),new BCryptPasswordEncoder().encode(users.getPassword()),role);
    }
}
